<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .title {
        	height: 35px;
        	line-height: 41px;
        	color: #333;
        	font-size: 12px;
        	padding: 0 15px;
        }
        .location_box {
        	display: -webkit-box;
        	display: -webkit-flex;
        	background-color: #fff;
        	padding: 15px;
        }
        .location_box .current_location {
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	padding-right: 5px;
        }
        .location_box .re_location {
			color: #999;
			font-size: 13px;
			display: -webkit-box;
			-webkit-box-align: center;
        }
        .location_box .re_location img {
        	height: 16px;
        	margin-right: 5px;
        	display: block;
        }
        .main ul li .li_box {
        	border-bottom: 1px solid #e8e8e8;
        	background-color: #fff;
        	padding: 17px 15px;
        }
        .main ul li:last-child .li_box {
        	border-bottom: none;
        }
        .main ul li .li_box .address {
        	font-size: 15px;
        	color: #333;
        	margin-bottom: 12px;
        }
        .main ul li .li_box .info {
        	font-size: 12px;
        	color: #999;
        }
        .main ul li .li_box .info .name {
        	margin-right: 8px;
        }
        .address_box {
            display: none;
        }
    </style>
</head>
<body>
    <div class="title">当前地址</div>
    <div class="location_box" onclick="fnChoseAddress();" tapmode>
    	<div class="current_location location_name"></div>
    	<div class="re_location" ><img src="../image/location2.png">重新定位</div>
    </div>
    <div class="address_box">
        <div class="title">收货地址</div>
        <div class="main">
        	<ul id="address_ul">
        		<!-- <li>
        			<div class="li_box">
        				<div class="address">泰翔商务楼泰翔商务楼泰翔商务楼</div>
        				<div class="info">
        					<span class="name">关先生</span>
        					<span>13333333333</span>
        				</div>
        			</div>
        		</li> -->
        	</ul>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="addressTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li onclick="fnAddressPoint({{=it[i]['lng']}},{{=it[i]['lat']}},'{{=it[i].addr_name}}');" tapmode>
            <div class="li_box">
                <div class="address">{{=it[i]['address_detail']}}</div>
                <div class="info">
                    <span class="name">{{=it[i]['username']}}</span>
                    <span>{{=it[i]['tel']}}</span>
                </div>
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        user = $api.getStorage('user_data');
        if(user) {
            fnGetAddress();
        }
        fnSerNowApoint();
        api.addEventListener({
            name: 'setPointEvent'
        }, function(ret, err) {
            if (ret) {
                var name = ret['value']['name'];
                var now_lng = ret['value']['lng'];
                var now_lat = ret['value']['lat'];
                $('.location_name').text(name);
                $api.setStorage('now_lat',now_lat);
                $api.setStorage('now_lng',now_lng);
                eventChangePoint(now_lat,now_lng);
            }
        });
    };

    function fnSerNowApoint() {
        var now_lng = $api.getStorage('now_lng');
        var now_lat  = $api.getStorage('now_lat');
        api.ajax({
            url: 'https://maps.googleapis.com/maps/api/geocode/json?language=zh-CN&latlng='+now_lat+','+now_lng+'&key=AIzaSyAdD-aDbsplFDJzsIFESWLnlBE7DFcZUWk',
            method: 'get',
            data: {
                values: {
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret['results'] && ret['results'][0]['formatted_address']) {
                    $('.location_name').text(ret['results'][0]['formatted_address']);
                }else {
                    $('.location_name').text('定位失败');
                }
            }
        });
    }

    function fnGetAddress(refresh) {
        openLoading();
        api.ajax({
            url : rootPath + '/api/user/get_user_address',
            data: {
                values: {
                    uid : user.id,
                    token:user.token,
                },
            },
            cache : true
        },function(ret,err){
            if(ret) {
                $('.address_box').show();
                if(refresh == 1) {
                    $('#address_ul').empty();
                }
                if(ret.status == 200) {
                    fnSetAddress(ret.data);
                }else {

                }
            }else {
                // showError(err.code,err.msg);
            }
            closeLoading();
        })

    }

    //装入预约数据
    function fnSetAddress(data_) {
        var listT = $api.byId('addressTpl');
        var listTFn = doT.template(listT.text);
        var html = listTFn(data_);
        var list = $api.byId('address_ul');
        $api.append(list, html);
        api.parseTapmode();
    }

    function fnChoseAddress() {
        api.openWin({
            name: 'chose_address',
            url: './chose_address.html',
        });
    }

    function fnAddressPoint(lng,lat,name) {
        $('.location_name').text(name);
        $api.setStorage('now_lat',lat);
        $api.setStorage('now_lng',lng);
        eventChangePoint(lat,lng);
        api.closeWin();
    }

    function eventChangePoint(lat,lng) {
        api.sendEvent({
            name: 'pointIsChangeEvent',
            extra: {
                lat: lat,
                lng: lng
            }
        });
    }
</script>
</html>
